package com.webmining.sherlock.util.search;

public class Path {
    
    /**
     * Calculates and absolute path using a relative path, replacing the
     * directories which contains <code>..</code>
     * @param path Relative path
     * @return Absolute path
     */
    public static String normalize(String path){
        if (path == null || path.length() == 0){
            return path;
        }
        int level;
        while ( (level = path.indexOf("/../")) != -1) {
            int last;
            int pos = path.indexOf('/');
            do {
                last = pos;
                pos = path.indexOf('/',pos+1);
            }
            while ( pos < level);
            path = path.substring(0,last) + path.substring(level + 3);
        }
        return path;
    }

}
